package cn.lua.lua.controller;

import cn.lua.lua.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.scripting.support.ResourceScriptSource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Transaction;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author <a href="mailto:18335553083@163.com">Mr_He</a>
 * @Copyright (c)</       b> HeC<br/>
 * @createTime 2018/7/21 20:36
 * @Description:
 */
@RestController
@RequestMapping("/")
public class Test1Controller {

    @Autowired
    private JedisPool jedisPool;

    @Autowired
    private RedisTemplate<String, Object> redis;

    @GetMapping("/get")
    public Result get(String type) {
        long start = System.currentTimeMillis();
        if (type.equals("1")) {
            for (int i = 0; i < 10000; i++) {
                int number = new Random(1000).nextInt();
                redis.opsForValue().set("" + number, number);
            }
        } else {
            DefaultRedisScript<Boolean> script = new DefaultRedisScript<>();
            ClassPathResource resource = new ClassPathResource("luas/get.lua");
            script.setScriptSource(new ResourceScriptSource(resource));
            script.setResultType(Boolean.class);
            redis.execute(script,null);
        }
        System.out.println(type+"总耗时："+(System.currentTimeMillis()-start));
        return new Result();
    }

    @GetMapping("/test1")
    public void test1(){
        Jedis jedis = jedisPool.getResource();
        Transaction transaction = jedis.multi();
        transaction.set("test","44444444444444");

    }

    @GetMapping("/test2")
    public void test2(){



    }

}
